package com.service1.db.mybatis_plus.db1.service

import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.base.model.service1.db.Model_Service1_MyBatisPlusDB
import com.service1.db.mybatis_plus.db1.mapper.MybatisPlusDB1Mapper
import org.springframework.stereotype.Service

@Service
class MyBatisPlusDB1Service(val myBatisPlusDB1Mapper: MybatisPlusDB1Mapper) {
    //单ID查询
    fun find(id: String): Model_Service1_MyBatisPlusDB {
        return myBatisPlusDB1Mapper.selectById(id)
    }

    //================================== select =====================================

    //多ID查询
    fun selectByIds(vararg ids: String): List<Model_Service1_MyBatisPlusDB> {
        return myBatisPlusDB1Mapper.selectBatchIds(ids.toList())
    }

    //多字段相等条件查询
    fun selectByMap(map: Map<String, Any>): List<Model_Service1_MyBatisPlusDB> {
        return myBatisPlusDB1Mapper.selectByMap(map)
    }

    //复杂查询
    fun selectByWrapper(): List<Model_Service1_MyBatisPlusDB> {
//        val wrapper_lambda = Wrappers.lambdaQuery<Model_Service1_MyBatisPlusDB>()
//        wrapper_lambda.eq(Model_Service1_MyBatisPlusDB::id, "a")
//        return myBatisPlusDB1Mapper.selectList(wrapper_lambda)
        val wrapper = Wrappers.query<Model_Service1_MyBatisPlusDB>()
        wrapper
            .like("column", "a") //like %a%
            .likeLeft("column", "a") //like %a
            .likeRight("column", "a") //like a%
            .notLike("column", "a") //not like %a%

            .eq("column", "a") //==
            .eq(false, "column", "a") //==，该条件不会拼接在最终执行的sql中
            .ne("column", "a") //!=
            .allEq(mapOf("column1" to "a", "column2" to "b")) //多列==
            .allEq(mapOf("column1" to "a", "column2" to "b"), false) //多列==，值为null时忽略条件
            .lt("column", 10) //<10
            .le("column", 10) //<=10
            .gt("column", 10) //>10
            .ge("column", 10) //>=10
            .between("column", 10, 100) //between 10 and 100
            .notBetween("column", 10, 100) //< 10 and >100
            .`in`("column", "a") //in("a")
            .notIn("column", "a") //not in("a")
            .inSql("column", "select id from t_test") //子查询
            .notInSql("column", "select id from t_test") //not 子查询
            .isNull("column") //is null
            .isNotNull("column") //is not null

            .orderBy(true, true, "column") //asc
            .orderBy(true, false, "column") //desc
            .orderByAsc("column") //asc
            .orderByDesc("column") //desc

            .apply("date_format(time_column,'%Y-%m-%d')={0}", "0000-00-00") //sql函数

            .or { it.gt("column", 10).lt("column", 100) } //or (column>10 and column<100)
            .and { it.eq("column", "a").or().eq("column", "b") } //and (column='a' or column='b')

            .nested { it.gt("column", 10).lt("column", 100) }.or() //正常嵌套 (column>10 and column<100) or

            .select("column1", "column2") //指定查询列

            .select("max(column2) maxColumn").groupBy("column").having("min(column1)>{0}", 100) //select max(column2) maxColumn from t_test group by column having min(column1)>100

            .eq(false, "column", "a") //不生效的条件
        return myBatisPlusDB1Mapper.selectList(wrapper)
    }

    //条数查询，无需设置select，自动count(1)
    fun selectCount(): Int {
        val wrapper = Wrappers.query<Model_Service1_MyBatisPlusDB>()
        return myBatisPlusDB1Mapper.selectCount(wrapper)
    }

    //分页查询
    fun selectPage(): List<Model_Service1_MyBatisPlusDB> {
        val wrapper = Wrappers.query<Model_Service1_MyBatisPlusDB>()
        val results = myBatisPlusDB1Mapper.selectPage(Page(1, 10), wrapper) //不统计分页数Page(1, 10, false)
        //        总页数:results.pages 总记录数:results.total
        return results.records
    }

    //自定义SQL查询
    fun selectSql(): List<Model_Service1_MyBatisPlusDB> {
        val wrapper = Wrappers.query<Model_Service1_MyBatisPlusDB>()
        return myBatisPlusDB1Mapper.selectSQL(wrapper)
    }

    //自定义SQL分页查询
    fun selectSqlPage(): List<Model_Service1_MyBatisPlusDB> {
        val wrapper = Wrappers.query<Model_Service1_MyBatisPlusDB>()
        val results = myBatisPlusDB1Mapper.selectSQLPage(Page(1, 10), wrapper) //不统计分页数Page(1, 10, false)
        //        总页数:results.pages 总记录数:results.total
        return results.records
    }

    //================================== insert =====================================

    //插入
    fun insert(model: Model_Service1_MyBatisPlusDB): Boolean {
        val rows = myBatisPlusDB1Mapper.insert(model)
        return rows > 0
    }

    //================================== update =====================================

    //根据ID
    fun updateBuId(model: Model_Service1_MyBatisPlusDB): Boolean {
        val rows = myBatisPlusDB1Mapper.updateById(model)
        return rows > 0
    }

    //根据条件
    fun updateWrapper(model: Model_Service1_MyBatisPlusDB): Boolean {
        val wrapper = Wrappers.update<Model_Service1_MyBatisPlusDB>()
        wrapper.eq("column", "a")
        val rows = myBatisPlusDB1Mapper.update(model, wrapper)
        return rows > 0
    }

    //根据条件，自定义字段
    fun updateWrapperSet(model: Model_Service1_MyBatisPlusDB): Boolean {
        val wrapper = Wrappers.update<Model_Service1_MyBatisPlusDB>()
        wrapper
            .eq("column", "a")
            .set("column", "b")
        val rows = myBatisPlusDB1Mapper.update(null, wrapper)
        return rows > 0
    }

    //================================== delete =====================================

    //根据ID
    fun deleteById(): Boolean {
        val rows = myBatisPlusDB1Mapper.deleteById("id")
        return rows > 0
    }

    //根据字段对应值
    fun deleteByMap(map: Map<String, Any>): Boolean {
        val rows = myBatisPlusDB1Mapper.deleteByMap(map)
        return rows > 0
    }

    //根据多IDs
    fun deleteByIds(models: List<Int>): Boolean {
        val rows = myBatisPlusDB1Mapper.deleteBatchIds(models)
        return rows > 0
    }

    //根据条件
    fun deleteWrapper(): Boolean {
        val wrapper = Wrappers.update<Model_Service1_MyBatisPlusDB>()
        val rows = myBatisPlusDB1Mapper.delete(wrapper)
        return rows > 0
    }
}
